package com.fx.eduservice.controller.frot;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fx.commonuntils.JwtUtils;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.commonuntils.commonresponse.CourstomOrderInfo;
import com.fx.eduservice.client.OrdersClient;
import com.fx.eduservice.entity.vo.EduCourse;
import com.fx.eduservice.request.CourseFrontVo;
import com.fx.eduservice.response.curstom.CourseWebVo;
import com.fx.eduservice.service.EduChapterService;
import com.fx.eduservice.service.EduCourseService;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CourseFrontController {


    final private EduCourseService courseService;


    final private EduChapterService chapterService;


   final private OrdersClient ordersClient;

    //1 条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public ReturnMessage<Object> getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                                    @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageParam = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageParam,courseFrontVo);
        //返回分页所有数据
        return ReturnMessageUtils.success(map);
    }

    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public ReturnMessage<Object> getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        ReturnMessage<Object> chapterVideoList = chapterService.getChapterVideo(courseId);
        //根据课程id和用户id查询当前课程是否已经支付过了
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        ArrayList<Object> objects = Lists.newArrayList(courseWebVo, chapterVideoList,buyCourse);
        return ReturnMessageUtils.success(objects);
    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourstomOrderInfo getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourstomOrderInfo courseWebVoOrder = new CourstomOrderInfo();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}












